
#include "TestResult.h"
#include "TestListener.h"
#include "TestCommon.h"
#include "Test.h"

#include <stdio.h>

CPPUNITLITE3_NS_BEGIN;

TestResult::TestResult ():
    m_iFailure(0), 
    m_iSuccess(0),
    m_bSuccess(true),
    m_iNum(0),
    m_iRepeat(1),
    m_elapsedTime(0)
{
}

void TestResult::addListener( TestListener* listener )
{
    m_listeners.push_back(listener);
}

void TestResult::removeListener( TestListener *listener )
{
}

void TestResult::setTestNumber(int num)
{
    m_iNum = num;
}

void TestResult::setRepeatTimes(int repeat)
{
    m_iRepeat = repeat;
}

void TestResult::testsStarted () 
{
    for(unsigned i = 0; i < m_listeners.size(); i++)
    {
        m_listeners[i]->testsStarted(m_iNum, m_iRepeat);
    }
    
    m_timer.startTimer();
}

void TestResult::testsEnded () 
{
    m_timer.endTimer();
    
    for(unsigned i = 0; i < m_listeners.size(); i++)
    {
        m_listeners[i]->testsEnded();
    }
}

void TestResult::StartTest(Test* test)
{
    m_bSuccess = true;
    m_elapsedTime = 0;
    
    for(unsigned i = 0; i < m_listeners.size(); i++)
    {
        m_listeners[i]->StartTest(test);
    }
}

void TestResult::EndTest(Test* test)
{
    if(wasSuccessful())
    {
        m_iSuccess++;
    }
    else
    {
        m_iFailure++;
    }
    
    for(unsigned i = 0; i < m_listeners.size(); i++)
    {
        m_listeners[i]->EndTest(test);
    }

    return;
}

bool TestResult::wasSuccessful() const 
{
    return m_bSuccess;
}

void TestResult::addElapsedTime(TimeInMillis elapsedTime)
{
    m_elapsedTime += elapsedTime;
}

void TestResult::addFailure (const TestFailure& failure) 
{
    m_bSuccess = false;

    for(unsigned i = 0; i < m_listeners.size(); i++)
    {
        m_listeners[i]->addFailure(failure);
    }
}

void TestResult::addSuccess()
{
    m_bSuccess = true;
}

int TestResult::getFailureCount()const
{
    return m_iFailure;
}

int TestResult::getSuccessCount()const
{
    return m_iSuccess;
}

int TestResult::getAllRun() const
{
    return getFailureCount() + getSuccessCount();
}

TimeInMillis TestResult::getElapsedTime() const
{
    return m_elapsedTime;
}

TimeInMillis TestResult::getTotalElapsedTime() const 
{
    return m_timer.getElapsedTime();
}

CPPUNITLITE3_NS_END;

